package com.sefer.dragonfly.client.core.domain.lifecycle;

import javax.management.MBeanOperationInfo;

import com.sefer.dragonfly.client.constants.State;
import com.sefer.dragonfly.client.core.domain.mbean.annotation.CMbeanMethod;

/**
 * <pre>
 * 生命周期管理抽象类，该类定义了监控的声明周期循环策略
 * 
 * init -----> running<------>stop
 *  ^                           |
 *  |                           | 
 *  |                           |
 *  |                           |
 *  |                           v
 * --------------------------destory
 * 
 * <pre>
 * 
 * @author xiaofeng 2011-9-9 上午09:59:43
 */
public abstract class BaseLifecycle implements Lifecycle {

	protected volatile State state = State.STOP;

	public void init() {
		this.setState(State.INITED);
	}

	public void start() {
		if (this.getState() == State.DESTROY) {
			init();
		}

		this.setState(State.RUNNING);
	}

	public void stop() {
		this.setState(State.STOP);
	}

	public void destory() {
		if (this.getState() != State.STOP) {
			stop();
		}
		this.setState(State.DESTROY);
	}

	@CMbeanMethod(description = "lifeCycle plugin", impact = MBeanOperationInfo.ACTION_INFO)
	public State getState() {
		return state;
	}

	@CMbeanMethod(description = "lifeCycle plugin", impact = MBeanOperationInfo.ACTION_INFO)
	public void setState(State state) {
		this.state = state;
	}

}
